﻿using Aker.DevTeam;
using DevExpress.XtraEditors;
using System;
using System.Windows.Forms;

namespace DevTeam
{
    public class LogManager
    {
        public static void Notify(Exception ex)
        {
            XtraMessageBox.Show(ex.Message, "Kullanıcı Hatası", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


        public static void LogToDB(Exception ex, string DevelopersNote = "")
        {
            string message = ex.Message.ToLower();


            int exceptionId = -1;
            try
            {

                exceptionId = Data.DM.ExecuteScalar("CatchException", ex.Message, ex.StackTrace, DevelopersNote, AppSession.Version, AppSession.UserInfo.UserId).ToIntNN();


            }
            catch
            {
                XtraMessageBox.Show("Bağlantınızda sorun olmadığından eminseniz ekran görüntüsü alıp software@karbel.com adresine e-posta gönderin.\r\n\r\n Hata: " + ex.Message + "\r\n\r\n" + ex.StackTrace, "Sistem Hatası");
                return;
            }


            if (
                    AppSession.UserInfo.UserId == 2 ||
                    message.Contains("concurrency exception") ||
                    message.Contains("timeout") ||
                    message.Contains("specified network name is no longer available") ||
                    message.Contains("zaman") ||
                    message.Contains("network") ||
                    message.Contains("tcp provider, error") ||
                    message.Contains("could not open a connection to sql server")
                )
                return;


            XtraMessageBox.Show(Environment.NewLine + ex.Message, "Sistem hatası - " + exceptionId.ToStringNN());

            string name = AppSession.UserInfo.Name;
            string icerik = "<span style='font-family:verdana;font-size:8'><br><b>Aker - Hata Kodu:&nbsp;</b>" + exceptionId + "<p>" + "&nbsp;" +
                            "<br><b>Programcı Notu:&nbsp;</b>" + DevelopersNote +
                                         "<p><p>" +
                            "<br><b>Kullanıcı:&nbsp;</b>" + name +
                             "<p><p>" +
                             "<br><b>Tarih:&nbsp;</b>" + DateTime.Now.ToString() +
                             "<p><p>" +
                             "<br><b>Kullanıcının Aldığı Hata:&nbsp;</b>" + ex.Message +
                             "<p><p>" +
                             "<br><b>StackTrace:&nbsp;</b>" + ex.StackTrace +
                             "<p><p>" +
                             "<br><b>Sorgu:&nbsp;</b>" + "select * from Exception where ExceptionId=" + exceptionId + "</span>";


            AppSession.Messenger.SendEmail("ce", "ce", "Aker - Hata", "kasimgulcan.karbel@gmail.com", "Aker - Hata", icerik, "");

        }
    }
}